package view;

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * 
 * @author Andy
 * @version 1
 * The form for adding ingredients.  This is called by the AddRecipePanel.
 *
 */
public class AddIngredientForm extends JPanel
{

    /**
     * The number of attributes to be entered.
     */
    private static final int ATTRIBUTES = 4;
    
    /**
     * The number of columns for the grid layout.
     */
    private static final int COLUMNS = 2;
    
    /**
     * Special index for Unit_Measure.
     */
    private static final int UNIT_MEASURE = 2;
    
    /**
     * The number of rows for the grid layout.
     */
    private static final int ROWS = 4;
    
    /**
     * The attributes to be added by this form.
     */
    private static final String[] INGREDIENT_ATTRIBUTES = {"Description", "Style_Prep", "Unit_Measure",
        "Quantity"};
    
    /**
     * The available units of measurement.
     */
    private static final String[] MEASUREMENTS = {"teaspoon", "tablespoon", "fluid ounce", "cup", "pint",
        "quart", "milliliter", "liter", "pound", "ounce", "milligram", "gram", "kilogram", "centimeter",
        "meter", "inch"};
    
    /**
     * An array of inputs enterd into the form.
     */
    private String[] my_inputs;
    
    /**
     * Text boxes added to the form.
     */
    private JTextField[] my_text_boxes;
    
    /**
     * The measurements ComboBox.
     */
    private JComboBox<String> my_measurements;
    
    /**
     * The center JPanel.
     */
    private JPanel my_center;
    
    /**
     * Constructs the ingredient adding form.
     */
    public AddIngredientForm()
    {
        my_inputs = new String[ATTRIBUTES];
        my_text_boxes = new JTextField[ATTRIBUTES];
        for (int i = 0; i < my_text_boxes.length; i++)
        {
            my_text_boxes[i] = new JTextField();
        }
        makeForm();
    }
    
    /**
     * Builds the form.
     */
    private void makeForm()
    {
        this.setLayout(new BorderLayout());
        makeCenter();
    }
    
    /**
     * Creates the center portion of the form.
     */
    private void makeCenter()
    {
        my_center = new JPanel();
        my_center.setLayout(new GridLayout(ROWS,COLUMNS));
        
        //Creates the entry labels and form.
        for (int i = 0; i < ATTRIBUTES; i++)
        {
            my_center.add(new JLabel(INGREDIENT_ATTRIBUTES[i]));
            if (i != UNIT_MEASURE) //Special case.  Index 2 is the units of measurement, which need a ComboBox.
            {
              my_center.add(my_text_boxes[i]);
            }
            else 
            {
              my_measurements = new JComboBox<String>();
              for (int j = 0; j < MEASUREMENTS.length; j++)
              {
                  my_measurements.addItem(MEASUREMENTS[j]);
              }
              my_center.add(my_measurements);
            }
        }
        this.add(BorderLayout.CENTER, my_center);     
    }
    
    /**
     * Returns the results of what was input into the form.
     * @return my_inputs
     */
    public String[] getInputs()
    {
        my_text_boxes[UNIT_MEASURE].setText((String) my_measurements.getSelectedItem());
        for (int i = 0; i < my_inputs.length; i++)
        {
          my_inputs[i] = my_text_boxes[i].getText();
        }
        for (int i = 0; i < my_text_boxes.length; i ++)
        {
            my_text_boxes[i].setText("");
        }
        return my_inputs;
    }
}
